﻿using System; 
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic; 
using System.Data;
using RBS.DBUtility;
using RBS.Common;
namespace RBS.Core.DAL  
{
	 	//换桌，预定，
		public partial class D_Order
	{
   		     
		public bool Exists(int Id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from D_Order");
			strSql.Append(" where ");
			                                       strSql.Append(" Id = @Id  ");
                            			SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
			parameters[0].Value = Id;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}
		
				
		
		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(RBS.Core.Model.D_Order model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into D_Order(");			
            strSql.Append("OrderState,OrderTime,OrderChannel,ContactUser,ContactMobile,OrderRemark,OrderAmount,RecAmount,DisAmount,AccAmount,PayType,PayTime,ReserveDate,ReserveTime,DineNum,H_Id,HotelName,DineTable,Dinecondition,U_Id,CreateTime,CreateUser,LastModifyDate,LastModifyUser");
			strSql.Append(") values (");
            strSql.Append("@OrderState,@OrderTime,@OrderChannel,@ContactUser,@ContactMobile,@OrderRemark,@OrderAmount,@RecAmount,@DisAmount,@AccAmount,@PayType,@PayTime,@ReserveDate,@ReserveTime,@DineNum,@H_Id,@HotelName,@DineTable,@Dinecondition,@U_Id,@CreateTime,@CreateUser,@LastModifyDate,@LastModifyUser");            
            strSql.Append(") ");            
            strSql.Append(";select @@IDENTITY");		
			SqlParameter[] parameters = {
			            new SqlParameter("@OrderState", SqlDbType.Int,4) ,            
                        new SqlParameter("@OrderTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@OrderChannel", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@ContactUser", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@ContactMobile", SqlDbType.NVarChar,20) ,            
                        new SqlParameter("@OrderRemark", SqlDbType.NVarChar,500) ,            
                        new SqlParameter("@OrderAmount", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@RecAmount", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@DisAmount", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@AccAmount", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@PayType", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@PayTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@ReserveDate", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@ReserveTime", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@DineNum", SqlDbType.Int,4) ,            
                        new SqlParameter("@H_Id", SqlDbType.Int,4) ,            
                        new SqlParameter("@HotelName", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@DineTable", SqlDbType.Int,4) ,            
                        new SqlParameter("@Dinecondition", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@U_Id", SqlDbType.Int,4) ,            
                        new SqlParameter("@CreateTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@CreateUser", SqlDbType.VarChar,50) ,            
                        new SqlParameter("@LastModifyDate", SqlDbType.DateTime) ,            
                        new SqlParameter("@LastModifyUser", SqlDbType.NVarChar,50)             
              
            };
			            
            parameters[0].Value = model.OrderState;                        
            parameters[1].Value = model.OrderTime;                        
            parameters[2].Value = model.OrderChannel;                        
            parameters[3].Value = model.ContactUser;                        
            parameters[4].Value = model.ContactMobile;                        
            parameters[5].Value = model.OrderRemark;                        
            parameters[6].Value = model.OrderAmount;                        
            parameters[7].Value = model.RecAmount;                        
            parameters[8].Value = model.DisAmount;                        
            parameters[9].Value = model.AccAmount;                        
            parameters[10].Value = model.PayType;                        
            parameters[11].Value = model.PayTime;                        
            parameters[12].Value = model.ReserveDate;                        
            parameters[13].Value = model.ReserveTime;                        
            parameters[14].Value = model.DineNum;                        
            parameters[15].Value = model.H_Id;                        
            parameters[16].Value = model.HotelName;                        
            parameters[17].Value = model.DineTable;                        
            parameters[18].Value = model.Dinecondition;                        
            parameters[19].Value = model.U_Id;                        
            parameters[20].Value = model.CreateTime;                        
            parameters[21].Value = model.CreateUser;                        
            parameters[22].Value = model.LastModifyDate;                        
            parameters[23].Value = model.LastModifyUser;                        
			   
			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);			
			if (obj == null)
			{
				return 0;
			}
			else
			{
				                    
            	return Convert.ToInt32(obj);
                                                                  
			}			   
            			
		}
		
		
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(RBS.Core.Model.D_Order model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update D_Order set ");
			                                                
            strSql.Append(" OrderState = @OrderState , ");                                    
            strSql.Append(" OrderTime = @OrderTime , ");                                    
            strSql.Append(" OrderChannel = @OrderChannel , ");                                    
            strSql.Append(" ContactUser = @ContactUser , ");                                    
            strSql.Append(" ContactMobile = @ContactMobile , ");                                    
            strSql.Append(" OrderRemark = @OrderRemark , ");                                    
            strSql.Append(" OrderAmount = @OrderAmount , ");                                    
            strSql.Append(" RecAmount = @RecAmount , ");                                    
            strSql.Append(" DisAmount = @DisAmount , ");                                    
            strSql.Append(" AccAmount = @AccAmount , ");                                    
            strSql.Append(" PayType = @PayType , ");                                    
            strSql.Append(" PayTime = @PayTime , ");                                    
            strSql.Append(" ReserveDate = @ReserveDate , ");                                    
            strSql.Append(" ReserveTime = @ReserveTime , ");                                    
            strSql.Append(" DineNum = @DineNum , ");                                    
            strSql.Append(" H_Id = @H_Id , ");                                    
            strSql.Append(" HotelName = @HotelName , ");                                    
            strSql.Append(" DineTable = @DineTable , ");                                    
            strSql.Append(" Dinecondition = @Dinecondition , ");                                    
            strSql.Append(" U_Id = @U_Id , ");                                    
            strSql.Append(" CreateTime = @CreateTime , ");                                    
            strSql.Append(" CreateUser = @CreateUser , ");                                    
            strSql.Append(" LastModifyDate = @LastModifyDate , ");                                    
            strSql.Append(" LastModifyUser = @LastModifyUser  ");            			
			strSql.Append(" where Id=@Id ");
						
SqlParameter[] parameters = {
			            new SqlParameter("@Id", SqlDbType.Int,4) ,            
                        new SqlParameter("@OrderState", SqlDbType.Int,4) ,            
                        new SqlParameter("@OrderTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@OrderChannel", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@ContactUser", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@ContactMobile", SqlDbType.NVarChar,20) ,            
                        new SqlParameter("@OrderRemark", SqlDbType.NVarChar,500) ,            
                        new SqlParameter("@OrderAmount", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@RecAmount", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@DisAmount", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@AccAmount", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@PayType", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@PayTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@ReserveDate", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@ReserveTime", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@DineNum", SqlDbType.Int,4) ,            
                        new SqlParameter("@H_Id", SqlDbType.Int,4) ,            
                        new SqlParameter("@HotelName", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@DineTable", SqlDbType.Int,4) ,            
                        new SqlParameter("@Dinecondition", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@U_Id", SqlDbType.Int,4) ,            
                        new SqlParameter("@CreateTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@CreateUser", SqlDbType.VarChar,50) ,            
                        new SqlParameter("@LastModifyDate", SqlDbType.DateTime) ,            
                        new SqlParameter("@LastModifyUser", SqlDbType.NVarChar,50)             
              
            };
						            
            parameters[0].Value = model.Id;                        
            parameters[1].Value = model.OrderState;                        
            parameters[2].Value = model.OrderTime;                        
            parameters[3].Value = model.OrderChannel;                        
            parameters[4].Value = model.ContactUser;                        
            parameters[5].Value = model.ContactMobile;                        
            parameters[6].Value = model.OrderRemark;                        
            parameters[7].Value = model.OrderAmount;                        
            parameters[8].Value = model.RecAmount;                        
            parameters[9].Value = model.DisAmount;                        
            parameters[10].Value = model.AccAmount;                        
            parameters[11].Value = model.PayType;                        
            parameters[12].Value = model.PayTime;                        
            parameters[13].Value = model.ReserveDate;                        
            parameters[14].Value = model.ReserveTime;                        
            parameters[15].Value = model.DineNum;                        
            parameters[16].Value = model.H_Id;                        
            parameters[17].Value = model.HotelName;                        
            parameters[18].Value = model.DineTable;                        
            parameters[19].Value = model.Dinecondition;                        
            parameters[20].Value = model.U_Id;                        
            parameters[21].Value = model.CreateTime;                        
            parameters[22].Value = model.CreateUser;                        
            parameters[23].Value = model.LastModifyDate;                        
            parameters[24].Value = model.LastModifyUser;                        
            int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		
		
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int Id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from D_Order ");
			strSql.Append(" where Id=@Id");
						SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
			parameters[0].Value = Id;


			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		
				/// <summary>
		/// 批量删除一批数据
		/// </summary>
		public bool DeleteList(string Idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from D_Order ");
			strSql.Append(" where ID in ("+Idlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
				
		
		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public RBS.Core.Model.D_Order GetModel(int Id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select Id, OrderState, OrderTime, OrderChannel, ContactUser, ContactMobile, OrderRemark, OrderAmount, RecAmount, DisAmount, AccAmount, PayType, PayTime, ReserveDate, ReserveTime, DineNum, H_Id, HotelName, DineTable, Dinecondition, U_Id, CreateTime, CreateUser, LastModifyDate, LastModifyUser  ");			
			strSql.Append("  from D_Order ");
			strSql.Append(" where Id=@Id");
						SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
			parameters[0].Value = Id;

			
			RBS.Core.Model.D_Order model=new RBS.Core.Model.D_Order();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			
			if(ds.Tables[0].Rows.Count>0)
			{
												if(ds.Tables[0].Rows[0]["Id"].ToString()!="")
				{
					model.Id=int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
				}
																																if(ds.Tables[0].Rows[0]["OrderState"].ToString()!="")
				{
					model.OrderState=int.Parse(ds.Tables[0].Rows[0]["OrderState"].ToString());
				}
																																if(ds.Tables[0].Rows[0]["OrderTime"].ToString()!="")
				{
					model.OrderTime=DateTime.Parse(ds.Tables[0].Rows[0]["OrderTime"].ToString());
				}
																																				model.OrderChannel= ds.Tables[0].Rows[0]["OrderChannel"].ToString();
																																model.ContactUser= ds.Tables[0].Rows[0]["ContactUser"].ToString();
																																model.ContactMobile= ds.Tables[0].Rows[0]["ContactMobile"].ToString();
																																model.OrderRemark= ds.Tables[0].Rows[0]["OrderRemark"].ToString();
																												if(ds.Tables[0].Rows[0]["OrderAmount"].ToString()!="")
				{
					model.OrderAmount=decimal.Parse(ds.Tables[0].Rows[0]["OrderAmount"].ToString());
				}
																																if(ds.Tables[0].Rows[0]["RecAmount"].ToString()!="")
				{
					model.RecAmount=decimal.Parse(ds.Tables[0].Rows[0]["RecAmount"].ToString());
				}
																																if(ds.Tables[0].Rows[0]["DisAmount"].ToString()!="")
				{
					model.DisAmount=decimal.Parse(ds.Tables[0].Rows[0]["DisAmount"].ToString());
				}
																																if(ds.Tables[0].Rows[0]["AccAmount"].ToString()!="")
				{
					model.AccAmount=decimal.Parse(ds.Tables[0].Rows[0]["AccAmount"].ToString());
				}
																																				model.PayType= ds.Tables[0].Rows[0]["PayType"].ToString();
																												if(ds.Tables[0].Rows[0]["PayTime"].ToString()!="")
				{
					model.PayTime=DateTime.Parse(ds.Tables[0].Rows[0]["PayTime"].ToString());
				}
																																				model.ReserveDate= ds.Tables[0].Rows[0]["ReserveDate"].ToString();
																																model.ReserveTime= ds.Tables[0].Rows[0]["ReserveTime"].ToString();
																												if(ds.Tables[0].Rows[0]["DineNum"].ToString()!="")
				{
					model.DineNum=int.Parse(ds.Tables[0].Rows[0]["DineNum"].ToString());
				}
																																if(ds.Tables[0].Rows[0]["H_Id"].ToString()!="")
				{
					model.H_Id=int.Parse(ds.Tables[0].Rows[0]["H_Id"].ToString());
				}
																																				model.HotelName= ds.Tables[0].Rows[0]["HotelName"].ToString();
																												if(ds.Tables[0].Rows[0]["DineTable"].ToString()!="")
				{
					model.DineTable=int.Parse(ds.Tables[0].Rows[0]["DineTable"].ToString());
				}
																																				model.Dinecondition= ds.Tables[0].Rows[0]["Dinecondition"].ToString();
																												if(ds.Tables[0].Rows[0]["U_Id"].ToString()!="")
				{
					model.U_Id=int.Parse(ds.Tables[0].Rows[0]["U_Id"].ToString());
				}
																																if(ds.Tables[0].Rows[0]["CreateTime"].ToString()!="")
				{
					model.CreateTime=DateTime.Parse(ds.Tables[0].Rows[0]["CreateTime"].ToString());
				}
																																				model.CreateUser= ds.Tables[0].Rows[0]["CreateUser"].ToString();
																												if(ds.Tables[0].Rows[0]["LastModifyDate"].ToString()!="")
				{
					model.LastModifyDate=DateTime.Parse(ds.Tables[0].Rows[0]["LastModifyDate"].ToString());
				}
																																				model.LastModifyUser= ds.Tables[0].Rows[0]["LastModifyUser"].ToString();
																										
				return model;
			}
			else
			{
				return null;
			}
		}
		
		
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select * ");
			strSql.Append(" FROM D_Order ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}
		
		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" * ");
			strSql.Append(" FROM D_Order ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}
		
		
		 #region  ExtensionMethod
        /// <summary>
        /// 获得查询分页数据
        /// </summary>
        public DataSet GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * FROM D_Order ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where 1=1 " + strWhere);
            }
            recordCount = Convert.ToInt32(DbHelperSQL.GetSingle(PagingHelper.CreateCountingSql(strSql.ToString())));
            return DbHelperSQL.Query(PagingHelper.CreatePagingSql(recordCount, pageSize, pageIndex, strSql.ToString(), filedOrder));
        }
		#endregion  ExtensionMethod

        /// <summary>
        /// 修改订单状态
        /// </summary>
        /// <param name="id"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        public bool UpdateOrderStatus(int id,int status)
        {
            var strSql = new StringBuilder();
            strSql.Append("update D_Order set "); 
            strSql.Append(" OrderState = @OrderState ");  
            strSql.Append(" where Id=@Id ");

            SqlParameter[] parameters = {
			            new SqlParameter("@Id", SqlDbType.Int,4) ,            
                        new SqlParameter("@OrderState", SqlDbType.Int,4)  
            }; 
            parameters[0].Value = id;
            parameters[1].Value = status; 
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 判断当前座位是否有订单
        /// </summary>
        /// <param name="sid"></param>
        /// <param name="time1"></param>
        /// <param name="time2"></param>
        /// <returns></returns>
        public bool Seat(int sid, DateTime time1, DateTime time2)
        {
            var strSql = new StringBuilder();
            strSql.Append("select count(1) from D_Order");
            strSql.Append(" where ");
            strSql.Append("  DineTable=@sid and OrderTime  between @time1 and @time2  and OrderState  in (2,4)   ");
            SqlParameter[] parameters = {
					new SqlParameter("@sid", SqlDbType.Int,4),
                    new SqlParameter("@time1", SqlDbType.DateTime,8),
                    new SqlParameter("@time2", SqlDbType.DateTime,8)
			};
            parameters[0].Value = sid;
            parameters[1].Value = time1;
            parameters[2].Value = time2;
            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }
             
           

	}
}

